package net.lcheng.shop.Controller;

import com.alibaba.fastjson.JSONObject;
import net.lcheng.model.*;
import net.lcheng.model.UserWithdrawalsRecord;
import net.lcheng.service.*;
import net.lcheng.shop.vo.CurrentUserModel;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * Created by zwr on 2016/7/23 0023.
 */
@Controller
public class UserWalletController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserAccountDetailsService userAccountDetailsService;
    @Autowired
    private UserIntegralService userIntegralService;
    @Autowired
    private UserCommissionService userCommissionService;
    @Autowired
    private UserWithdrawalsRecordService userWithdrawalsRecordService;
    /**账号明细*/
    @RequestMapping(value = "user/account_details", method = RequestMethod.GET)
    public String userWalletDetailGet(Model model) {
        //获取当前用户
        Subject currentUser= SecurityUtils.getSubject();
        //判断当前用户有没有登录
        if(currentUser.isAuthenticated()) {
            Session session = currentUser.getSession();
            CurrentUserModel currentUserModel = JSONObject.parseObject(session.getAttribute("user").toString(), CurrentUserModel.class);
            User user=userService.getUserByUserId(currentUserModel.getUserId());
            model.addAttribute("user",user);
            //获取账号明细
            List<UserAccountDetails>userWalletDetailList= userAccountDetailsService.getUserWalletDetailsByUserId(currentUserModel.getUserId());
            model.addAttribute("userWalletDetails",userWalletDetailList);
        }
        return "account_details";
    }
    /**积分明细*/
    @RequestMapping(value = "user/integral", method = RequestMethod.GET)
    public String userIntegralGet(Model model) {
        //获取当前用户
        Subject currentUser= SecurityUtils.getSubject();
        //判断当前用户有没有登录
        if(currentUser.isAuthenticated()) {
            Session session = currentUser.getSession();
            CurrentUserModel currentUserModel = JSONObject.parseObject(session.getAttribute("user").toString(), CurrentUserModel.class);
            User user=userService.getUserByUserId(currentUserModel.getUserId());
            model.addAttribute("user",user);
            //获取积分明细
            List<UserIntegral>userIntegralList=userIntegralService.getUserIntegralByUserId(user.getUserId());
            model.addAttribute("userIntegralList",userIntegralList);
        }
        return "integral";
    }
    /**佣金收入*/
    @RequestMapping(value = "user/commission", method = RequestMethod.GET)
    public String userCommissionGet(Model model) {
        //获取当前用户
        Subject currentUser= SecurityUtils.getSubject();
        //判断当前用户有没有登录
        if(currentUser.isAuthenticated()) {
            Session session = currentUser.getSession();
            CurrentUserModel currentUserModel = JSONObject.parseObject(session.getAttribute("user").toString(), CurrentUserModel.class);
            User user=userService.getUserByUserId(currentUserModel.getUserId());
            model.addAttribute("user",user);
            //获取佣金明细
            List<UserCommission>userCommissionList=userCommissionService.getUserCommissionByUserId(user.getUserId());
            model.addAttribute("userCommissionList",userCommissionList);
        }
        return "commission";
    }
    /**提现记录*/
    @RequestMapping(value = "user/withdrawals_record", method = RequestMethod.GET)
    public String userWithdrawalsRecordGet(Model model) {
        //获取当前用户
        Subject currentUser= SecurityUtils.getSubject();
        //判断当前用户有没有登录
        if(currentUser.isAuthenticated()) {
            Session session = currentUser.getSession();
            CurrentUserModel currentUserModel = JSONObject.parseObject(session.getAttribute("user").toString(), CurrentUserModel.class);
            User user=userService.getUserByUserId(currentUserModel.getUserId());
            model.addAttribute("user",user);
            //获取佣金明细
            List<UserWithdrawalsRecord>userWithdrawalsRecordList=userWithdrawalsRecordService.getUserWithdrawalsRecordByUserId(user.getUserId());
            model.addAttribute("userWithdrawalsRecordList",userWithdrawalsRecordList);
        }
        return "withdrawals_record";
    }
//    /**提现详情*/
//    @RequestMapping(value = "user/withdrawals_detail", method = RequestMethod.GET)
//    public String userWithdrawalsDetailGet(Model model) {
//        Subject currentUser= SecurityUtils.getSubject();
//        if(currentUser.isAuthenticated()) {
//            Session session = currentUser.getSession();
//            CurrentUserModel currentUserModel = JSONObject.parseObject(session.getAttribute("user").toString(), CurrentUserModel.class);
//            User user=userService.getUserByUserId(currentUserModel.getUserId());
//            model.addAttribute("user",user);
//            //获取佣金明细
//            List<UserWithdrawalsRecord>userWithdrawalsRecordList=userWithdrawalsRecordService.getUserWithdrawalsRecordByUserId(user.getUserId());
//            model.addAttribute("userWithdrawalsRecordList",userWithdrawalsRecordList);
//        }
//        return "withdrawals_detail";
//    }
}
